from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QPushButton, 
                           QVBoxLayout, QStackedWidget, QTableWidget,
                           QTableWidgetItem, QHeaderView, QMessageBox)
from PyQt5.QtCore import Qt
from .info_input_page import InfoInputPage
from .read_card_page import ReadCardPage
from .person_manage_page import PersonManagePage
from datetime import datetime
import requests

class MainPage(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setup_ui()
        
    def setup_ui(self):
        layout = QHBoxLayout(self)
        
        # 左侧按钮面板
        left_panel = QVBoxLayout()
        self.write_btn = QPushButton("写卡录入")
        self.read_btn = QPushButton("读卡")
        self.manage_btn = QPushButton("人员管理")
        
        # 设置按钮样式
        for btn in [self.write_btn, self.read_btn, self.manage_btn]:
            btn.setMinimumHeight(40)
            btn.setCheckable(True)
            left_panel.addWidget(btn)
        
        # 默认选中写卡录入
        self.write_btn.setChecked(True)
        
        # 连接按钮信号
        self.write_btn.clicked.connect(lambda: self.switch_page(0))
        self.read_btn.clicked.connect(lambda: self.switch_page(1))
        self.manage_btn.clicked.connect(lambda: self.switch_page(2))
        
        left_panel.addStretch()
        
        # 创建右侧堆叠窗口
        self.stack = QStackedWidget()
        
        # 添加写卡录入页面
        self.info_input_page = InfoInputPage()
        self.stack.addWidget(self.info_input_page)
        
        # 添加读卡页面
        self.read_page = ReadCardPage()
        self.stack.addWidget(self.read_page)
        
        # 添加人员管理页面
        self.manage_page = PersonManagePage()
        self.stack.addWidget(self.manage_page)
        
        # 将左侧面板和堆叠窗口添加到主布局
        left_widget = QWidget()
        left_widget.setLayout(left_panel)
        left_widget.setMaximumWidth(150)  # 限制左侧面板宽度
        
        layout.addWidget(left_widget)
        layout.addWidget(self.stack)
        
    def switch_page(self, index):
        # 更新按钮状态
        buttons = [self.write_btn, self.read_btn, self.manage_btn]
        for i, btn in enumerate(buttons):
            btn.setChecked(i == index)
        
        # 切换页面
        self.stack.setCurrentIndex(index) 